package com.gujiangbo.juc;

import java.util.concurrent.TimeUnit;

/**
 * @Author: gujiangbo.co
 * @Contact: 646832012@qq.com
 * @Date: 2022/6/10 10:13
 * @Version: 1.0
 * @Description:
 */
public class InterRuptDemo2 {

    public static void main(String[] args) {

        Thread t1 = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println(Thread.currentThread().getName() + " 中断标志位:" + Thread.currentThread().isInterrupted() + " 程序终止");
                    break;
                }
                try {
                    Thread.sleep(20);//sleep 会将中断标志位设置为false
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    e.printStackTrace();
                }
                System.out.println("-----hello interrupter---");

            }
        }, "t1");

        t1.start();

        //暂停10秒钟线程
        try{
            TimeUnit.MILLISECONDS.sleep(30);} catch (InterruptedException e) {e.printStackTrace();}
        
        new Thread(()->{t1.interrupt();},"t2").start();

    }
}
